//---------------------------------------------------------------------
// <copyright file="EFOracleVersion.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------

namespace EFOracleOdpProvider
{
    using System;

    /// <summary>
    /// This enum describes the current storage version
    /// </summary>
    internal enum EFOracleVersion
    {
        /// <summary>
        /// Oracle10g
        /// </summary>
        Oracle10g = 10,

        /// <summary>
        /// Oracle 11g
        /// </summary>
        Oracle11g = 11,

        // higher versions go here
    }

    /// <summary>
    /// This class is a simple utility class that determines the version from the 
    /// connection
    /// </summary>
    internal static class EFOracleVersionUtils
    {
        /// <summary>
        /// Get the version from the connection.
        /// </summary>
        /// <param name="connection">current connection</param>
        /// <returns>version for the current connection</returns>
        internal static EFOracleVersion GetStorageVersion(EFOracleOdpConnection connection)
        {
            string serverVersion = connection.ServerVersion;
            if (serverVersion.StartsWith("10.")) 
            {
                return EFOracleVersion.Oracle10g;
            }
            else if(serverVersion.StartsWith("11."))
            {
                return EFOracleVersion.Oracle11g;
            }

            throw new ArgumentException("Could not determine storage version; " +
                    "a valid storage connection or a version hint is required.");
        }

        internal static string GetVersionHint(EFOracleVersion version)
        {
            switch(version)
            {
                case EFOracleVersion.Oracle10g:
                    return EFOracleOdpProviderManifest.TokenOracle10g;

                case EFOracleVersion.Oracle11g:
                    return EFOracleOdpProviderManifest.TokenOracle11g;

                default:
                    throw new ArgumentException("Could not determine storage version; " +
                            "a valid storage connection or a version hint is required.");
            }
        }

        internal static EFOracleVersion GetStorageVersion(string versionHint)
        {
            if (!string.IsNullOrEmpty(versionHint))
            {
                switch (versionHint)
                {
                    case EFOracleOdpProviderManifest.TokenOracle10g:
                        return EFOracleVersion.Oracle10g;

                    case EFOracleOdpProviderManifest.TokenOracle11g:
                        return EFOracleVersion.Oracle11g;
                }
            }
            
            throw new ArgumentException("Could not determine storage version; " +
                    "a valid storage connection or a version hint is required.");
        }

        internal static bool IsVersionX(EFOracleVersion storageVersion)
        {
            return storageVersion == EFOracleVersion.Oracle10g ||
                storageVersion == EFOracleVersion.Oracle11g;
        }
    }
}
